/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-10 16:49:18
 * @LastEditTime: 2023-01-07 11:53:15
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import { Modal } from 'antd';
import {
  SET_SELECTED_ITEM,
  SET_CDVOLUM_LIST,
  ADD_CDVOLUM,
  EDIT_CDVOLUM,
  DELETE_CDVOLUM
} from '../constants';
import { CDVolumManageReducer } from '../reducers/CDVolumManageReducer';
import {
  fetchDiskTypeListByPage,
  addDiskType,
  editDiskType,
  deleteDiskType
} from '@/api/archives/export';
const initialState = {
  selectItems: [],
  cdVolumList: []
};
const CDVolumManageContext = createContext();
const CDVolumManageContextProvider = (props) => {
  const [state, dispatch] = useReducer(CDVolumManageReducer, initialState);
  const actions = {
    /**
     * @description: 设置表格中选中条目的key
     * @param {*} selectArr
     * @return {*}
     */
    setSelectKey(selectArr) {
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },
    /**
     * @description: 请求档案分类列表
     * @param {object} params 请求参数
     * @return {*}
     */
    getCDVolumManageList(params) {
      return new Promise((resolve, reject) => {
        fetchDiskTypeListByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_CDVOLUM_LIST, data: data.results });
            resolve(res);
          })
          .catch((err) => {
            dispatch({ type: SET_CDVOLUM_LIST, data: [] });
            reject(err);
          });
      });
    },
    /* 增加报表 */
    addDiskTypeList(params) {
      return new Promise((resolve, reject) => {
        addDiskType(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: ADD_CDVOLUM, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /* 修改报表 */
    editDiskList(params) {
      return new Promise((resolve, reject) => {
        editDiskType(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: EDIT_CDVOLUM, data });
            resolve(res);
          })
          .catch((err) => reject(err));
      });
    },
    /**
     * @description: 删除
     * @param {*} id 删除的id
     * @param {*} content 弹窗展示内容
     * @return {*}
     */
    deleteDiskFunc(id, content = '确认删除该光盘容量？') {
      Modal.confirm({
        title: '删除',
        content,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteDiskType(ids).then((res) => {
            const data = res.data.map((id) => parseInt(id, 10));
            dispatch({ type: DELETE_CDVOLUM, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    }
  };
  useEffect(() => {}, []);
  return (
    <CDVolumManageContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </CDVolumManageContext.Provider>
  );
};
export const useCDVolumManageContext = () => {
  return useContext(CDVolumManageContext);
};
export { CDVolumManageContext, CDVolumManageContextProvider };
